﻿using System;

namespace GameLibrary
{    
    using Interfaces;
    [Serializable]
    public abstract class AbstractMapObject: AbstractGameObject, 
                                             IMapObjectWithEventHandler
    {
        #region variables

        #region location

        public Position Location { get; private set; }
        public void SetLocation(Position pos)
        {
            Position old = this.Location;
            OnLocationChanging(this);
            this.Location = pos;
            OnLocationChange(old, pos);
        }

        #endregion

        #region owner id

        public uint OwnerID { get; private set; }
        public virtual void SetOwnerID(uint ownerID)
        {
            OnOwnerChanging(this.OwnerID, ownerID);
            var old = this.OwnerID;           
            this.OwnerID = ownerID;
            OnOwnerChanged(old, ownerID);
        }

        #endregion

        #endregion

        #region constructor

        public AbstractMapObject(Position location, uint ownerID)
            : base()
        {
            SetLocation(location);
            SetOwnerID(ownerID);
        }

        #endregion

        #region IMapObject Members

        public event EventHandler<Events.LocationChangedEventArgs> LocationChanged;
        protected virtual void OnLocationChange(Position oldPosition, Position newPosition)
        {
            EventHandler<Events.LocationChangedEventArgs> h = this.LocationChanged;
            if (h != null)
                h(this, new Events.LocationChangedEventArgs(this,oldPosition, newPosition));
        //    else
        //        throw new Exceptions.EventHandlerNullException("Location change event handler null.");
        }

        public event EventHandler<Events.OwnershipChangingEventArgs> OwnerChanging;
        protected virtual void OnOwnerChanging(uint oldID, uint newID)
        {
            EventHandler<Events.OwnershipChangingEventArgs> h = this.OwnerChanging;
            if (h != null)
                h(this, new Events.OwnershipChangingEventArgs(oldID, newID, this));
         //   else
         //       throw new Exceptions.EventHandlerNullException("OwnerChanging event handler null.");
        }
        
        public event EventHandler<Events.OwnershipChangedEventArgs> OwnerChanged;
        protected virtual void OnOwnerChanged(uint oldID, uint newID)
        {
            EventHandler<Events.OwnershipChangedEventArgs> h = this.OwnerChanged;
            if (h != null)
                h(this, new Events.OwnershipChangedEventArgs(this, oldID, newID));
         //   else
         //       throw new Exceptions.EventHandlerNullException("Ownership change event handler null.");
        }

        public event EventHandler<Events.LocationChangingEventArgs> LocationChanging;
        protected virtual void OnLocationChanging(IMapObject obj)
        {
            EventHandler<Events.LocationChangingEventArgs> h = this.LocationChanging;
            if (h != null)
                h(this, new Events.LocationChangingEventArgs(this));
        }
        #endregion
    }
}
